using System;
using System.Collections;
using System.Collections.Generic;
using ZoneFiveSoftware.Common.Visuals;

namespace FilteredStatistics.View
{
    class StatisticsTreeListDistributor
    {
        public StatisticsTreeListDistributor()
        {
        }

        public void RegisterStatisticsTreeList(ExtendedTreeList list)
        {
            if (!m_RegisteredTreeLists.Contains(list))
            {
                m_RegisteredTreeLists.Add(list);

                SetupTreeList(list);
            }
        }

        public void UnregisterStatisticsTreeList(ExtendedTreeList list)
        {
            if (m_RegisteredTreeLists.Contains(list))
            {
                m_RegisteredTreeLists.Remove(list);
            }
        }

        public void ThemeChanged(ITheme visualTheme)
        {
            foreach (ExtendedTreeList list in m_RegisteredTreeLists)
            {
                list.ThemeChanged(visualTheme);
            }
        }

        private void SetupTreeList(ExtendedTreeList list)
        {
            list.RowDataRenderer = new FilteredStatisticsRowDataRenderer(list);
            list.LabelProvider = new FilteredStatisticsTreeListLabelProvider();
            list.ThemeChanged(PluginMain.GetApplication().VisualTheme);
            list.RowData = m_CurrentRowData;
            list.Selected = m_LastSelection;
            list.SetSortIndicator(m_SortColumnId, m_SortAscending);
            list.Columns.Clear();

            if (m_ColumnsDisplayed != null)
            {
                foreach (TreeList.Column column in m_ColumnsDisplayed)
                {
                    list.Columns.Add(column);
                }
            }

            list.NumLockedColumns = m_NumLockedColumns;
        }

        public void SetSortIndicator(string columnId, bool bAscending)
        {
            m_SortColumnId = columnId;
            m_SortAscending = bAscending;

            foreach (ExtendedTreeList list in m_RegisteredTreeLists)
            {
                list.SetSortIndicator(columnId, bAscending);
            }
        }

        public List<TreeList.TreeListNode> RowData
        {
            set
            {
                m_CurrentRowData = value;

                foreach (ExtendedTreeList list in m_RegisteredTreeLists)
                {
                    list.RowData = m_CurrentRowData;
                }
            }
        }

        public IList Selected
        {
            set
            {
                m_LastSelection = value;

                foreach (ExtendedTreeList list in m_RegisteredTreeLists)
                {
                    list.Selected = m_LastSelection;
                }
            }
        }

        public IList<TreeList.Column> Columns
        {
            set
            {
                m_ColumnsDisplayed = value;

                foreach (ExtendedTreeList list in m_RegisteredTreeLists)
                {
                    list.Columns.Clear();

                    foreach (TreeList.Column column in m_ColumnsDisplayed)
                    {
                        list.Columns.Add(column);
                    }
                }
            }
        }

        public int NumLockedColumns
        {
            set
            {
                m_NumLockedColumns = value;

                foreach (ExtendedTreeList list in m_RegisteredTreeLists)
                {
                    list.NumLockedColumns = m_NumLockedColumns;
                }
            }
        }

        private List<ExtendedTreeList> m_RegisteredTreeLists = new List<ExtendedTreeList>();
        private List<TreeList.TreeListNode> m_CurrentRowData = null;
        private IList m_LastSelection = null;
        private IList<TreeList.Column> m_ColumnsDisplayed = null;
        private String m_SortColumnId = "";
        private int m_NumLockedColumns = 0;
        private bool m_SortAscending = true;
    }
}
